﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;

namespace SchoolManagementSystem
{
    public partial class Log : Form
    {
        public Log()
        {
            InitializeComponent();
            Style.setDatagridviewStyle(dataGridView1);
        }

        private DateTime ConvertFromUnixTimestamp(double timestamp)
        {
            DateTime origin = new DateTime(1970, 1, 1, 0, 0, 0, 0);
            return origin.AddSeconds(timestamp);
        }


        private double ConvertToUnixTimestamp(DateTime date)
        {
            DateTime origin = new DateTime(1970, 1, 1, 0, 0, 0, 0);
            TimeSpan diff = date - origin;
            return Math.Floor(diff.TotalSeconds);
        }

        private DateTime UnixTimeStampToDateTime(double unixTimeStamp)
        {
            System.DateTime dtDateTime = new DateTime(1970, 1, 1, 0, 0, 0, 0);
            dtDateTime = dtDateTime.AddSeconds(unixTimeStamp).ToLocalTime();
            return dtDateTime;
        }

        private void button1_Click(object sender, EventArgs e)
        {
            String akkun = textBox1.Text.ToString();
            String fromDate = dateTimePicker1.Value.ToString("MM/dd/yyyy");
            fromDate += " 00:00:00";
            String untilDate = dateTimePicker2.Value.ToString("MM/dd/yyyy");
            untilDate += " 23:59:59";

            String query;

            if (akkun == "")
            {
                query = "select time as \"Waktu\",Account.acc_name as \"Akun\",action as \"Action\" from Log,Account where time between '" + fromDate + "' and '" + untilDate + "' and Account.acc_id=Log.acc_id";
            }
            else
            {
                query = "select time as \"Waktu\",Account.acc_name as \"Akun\",action as \"Action\" from Log,Account where (time between '" + fromDate + "' and '" + untilDate + "') and (Account.user_name like '%" + akkun + "%') and Account.acc_id=Log.acc_id";
            }

            DataGridViewRow dataGridViewRow = this.dataGridView1.RowTemplate;
            dataGridViewRow.DefaultCellStyle.BackColor = Color.White;
            dataGridViewRow.Height = 35;
            dataGridViewRow.MinimumHeight = 20;

            DataTable dt = Database.getInstance().tes(query);
            dt.DefaultView.Sort = "Waktu ASC";

            dataGridView1.DataSource = dt;

            dataGridView1.RowHeadersWidthSizeMode = System.Windows.Forms.DataGridViewRowHeadersWidthSizeMode.AutoSizeToDisplayedHeaders;
            String rowNumber = "1";
            foreach (DataGridViewRow row in dataGridView1.Rows)
            {
                if (row.IsNewRow) continue;
                row.HeaderCell.Value = rowNumber;
                rowNumber = (int.Parse(rowNumber) + 1).ToString();
            }
            dataGridView1.AutoResizeRowHeadersWidth(
                DataGridViewRowHeadersWidthSizeMode.AutoSizeToAllHeaders);
        }

        private void Log_Load(object sender, EventArgs e)
        {

        }
    }
}
